//
//  ActiveDeckViewController.m
//  ActiveDeck
//
//  Created by Chiao-Wei Liu on 2011/1/16.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ActiveDeckViewController.h"
#import "HomeViewController.h"
#import <CommonCrypto/CommonCryptor.h>
#import "AD_Slide.h"
#import "AD_Kiosk.h"
#import "Init.h"
#import "SlideViewController.h"
#import "HttpRequest.h"
#import "GDataXMLNode.h"
#import "StatusUpdater.h"
#import "Init.h"
#import "AD_ContainerOB.h"
#import "PageViewController.h"
#import <QuartzCore/CALayer.h>

#define degreesToRadian(x) (M_PI * (x) / 180.0)

NSString *m_CurrentStatus;
NSTimer *Timer_Status;
HttpRequest *requestOB;

BOOL m_justJump = NO;
BOOL m_GetStatusOld = TRUE;
//NSString *m_Channel_ID; 
BOOL m_idle = FALSE;
int SessionCount = 0;
StatusUpdater *updater;
StatusUpdater *secondaryUpdater;
BOOL isGoingBackwards = FALSE;

@implementation ActiveDeckViewController
@synthesize homeMode, slideVCSets, userData, idleTimer , updateTimer,initOB;
@synthesize numberIncorrect, numberCorrect, rotateMovie;
@synthesize allPages,contController,VC_Page,upcomingSlideIndex;

@synthesize pdfFactory, cancelPDFButton;
@synthesize hasMovedOn, pdfProgressBar, savingPDFView, savingPDFLabel, pdfPresentationLabel;


#pragma mark -
#pragma mark Save all the note and photo into document

-(void)SaveIntoFile
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    
    [contController SaveContainerOBtoXML];
    
    [pool release];
}



#pragma mark -
#pragma mark Create PDF Functions
-(void)showPDFProgress{
    [updater setUpdateStatus:NO];
    [[self view] addSubview:savingPDFView];
    
}

-(void)hidePDFProgress{
    [savingPDFView removeFromSuperview];
    [updater setUpdateStatus:YES];
}

-(void)exportToPDF:(NSMutableArray*)currentPosition{
    [self pdfBarUpdate:@"0"];
    pdfPresentationLabel.text = homeMode.presentID.text;
    pdfFactory = [[PDFFactory alloc] init:homeMode.presentID.text :homeMode.initOB.mu_Arr_Slides :contController.ContainOB :self :homeMode.cont_Username:currentPosition];
    [pdfFactory initPDF]; 
}

-(void)pdfBarUpdate:(NSString*)progress{
    pdfProgressBar.progress = [progress floatValue];
    savingPDFLabel.text = [NSString stringWithFormat:@"Saving to PDF... %d%%", (int)([progress floatValue] * 100)];
}

-(IBAction)cancelPDF:(id)sender{
    [pdfFactory setIsCancelled:YES];
}

#pragma mark -
#pragma mark Leave the Old Presesntation and Start new One

-(void)newPresentation
{
    [[homeMode Bt_About] setEnabled:YES];
    [[homeMode Bt_Logout] setEnabled:YES];
    [homeMode updatePIDList];
    [homeMode hideAll:TRUE];
    [updater stop];
    [updater release];
    
    
    //NSLog(@"Updater's Count %d", [updater retainCount]);
    for (int i=0; i<[slideVCSets count]; i++) {
        
        SlideViewController *slideVC = [slideVCSets objectAtIndex:i];
        if((NSNull *)slideVC != [NSNull null])
        {
            [slideVC.view removeFromSuperview];
            [slideVCSets replaceObjectAtIndex:i withObject:[NSNull	null]];
        }
    }
    
    
    slideVCSets = nil;
    numberCorrect = 0;
    numberIncorrect = 0;
    
    [contController SaveContainerOBtoXML];
    [contController release];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	requestOB = [HttpRequest alloc];
	
    
	
	[homeMode setDelegate:self];
	[self.view addSubview:homeMode.view];
    [homeMode viewDidAppear:TRUE];
    
}

-(void)ShowCamera:(PageViewController *)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    
    [self presentModalViewController:picker animated:YES];
    VC_Page = sender;
}


- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    
    [self dismissModalViewControllerAnimated:YES];
    
    [[NSBundle mainBundle]loadNibNamed:@"View_AddPhotoNote" owner:VC_Page options:nil];
    VC_Page.SavedImage.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [VC_Page AddPhotoNote];
    
    [[VC_Page photoNoteWarning] setHidden:!hasMovedOn];
    
    hasMovedOn = NO;
}

#define idleTime 1

-(void)UpdateStatus
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    
    if(homeMode.initOB.Conf_Kiosk.enabled)
    {
        
        [self performSelectorOnMainThread:@selector(resetIdleTimer) withObject:nil waitUntilDone:NO];
        
        if(homeMode.initOB.Conf_Kiosk.AutoUpdate)
            [self performSelectorOnMainThread:@selector(resetUpdateTimer) withObject:nil waitUntilDone:NO];
    }
    else
    {
        updater = [StatusUpdater alloc];
        NSLog(@"Updater RetainCount:%d", [updater retainCount]);
        
        [updater setDelegate:self];
        NSLog(@"Updater RetainCount:%d", [updater retainCount]);
        
        [updater setInitialStatusPath:[NSString stringWithFormat:@"%@/Config.svc/GetCurrentState/%d",[HttpRequest GetSvcURL], homeMode.initOB.PID]];
        [updater setSubscriptionPath:[NSString stringWithFormat:@"%@/Sync.svc/subscribe", [HttpRequest GetSvcURL]]];
        
        [updater initialUpdater:[homeMode.presentID.text intValue] 
                      loginInfo:homeMode.loginXML
                       AuthNode:homeMode.AuthNode];
    }   
    [pool release];
}

-(void)UpdateSecondary{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    
    if(homeMode.initOB.Conf_Kiosk.enabled)
    {
        
        [self performSelectorOnMainThread:@selector(resetIdleTimer) withObject:nil waitUntilDone:NO];
        
        if(homeMode.initOB.Conf_Kiosk.AutoUpdate)
            [self performSelectorOnMainThread:@selector(resetUpdateTimer) withObject:nil waitUntilDone:NO];
    }
    
    
    
    if(homeMode.initOB.hasSecondarySubscription){
        secondaryUpdater = [StatusUpdater alloc];
        
        [secondaryUpdater setDelegate:self];
        
        [secondaryUpdater setSubscriptionPath: [NSString stringWithFormat:@"%@/subscribe", homeMode.initOB.webServiceEndPoint]];
        
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSString *ACTIVEDECKKIOSK = [prefs stringForKey:@"Kiosk"];
        
        int kioskID = 0;
        if(ACTIVEDECKKIOSK){
            kioskID = [ACTIVEDECKKIOSK intValue];
        }
        
        secondaryUpdater.m_Channel_ID = [NSString stringWithFormat:homeMode.initOB.channelFormat, homeMode.presentID.text, kioskID];
        //[secondaryUpdater setChannelFormat:homeMode.initOB.channelFormat];
        
        [secondaryUpdater initialUpdater:[homeMode.presentID.text intValue] loginInfo:homeMode.loginXML AuthNode:homeMode.AuthNode initialStatus:NO];
    }
    
    [pool release];
}

-(void)updateSecondaryChannelID:(int)key{
    if(secondaryUpdater){
        secondaryUpdater.m_Channel_ID = [NSString stringWithFormat:homeMode.initOB.channelFormat, homeMode.presentID.text, key];
    }
}

-(int)SearchSectionIdex:(int)SectionID
{
    int i=0;
    
    for(AD_Slide *slideOB in homeMode.initOB.mu_Arr_Slides)
	{
		if(slideOB.SlideID != SectionID)
			i++;
		else
			break;
	}
    
    if( i >= [homeMode.initOB.mu_Arr_Slides count])
        return 0;
    
    return i;
}


-(int)GetSlideIndexByID:(int)SectionID
{
	NSLog(@"RetainCount:%d", [homeMode.initOB.mu_Arr_Slides retainCount]);
    NSLog(@"SectionCount:%d", [homeMode.initOB.mu_Arr_Slides count]);
    
    
    //SectionID = 257;
	//***************************TEST CODE Index Start**********************
	/*
     if (SessionCount < 7) {
     return SessionCount++;
     }
     else {
     return SessionCount--;
     }*/
    //userData = homeMode.AuthNode;
    
	//return 1;
    
    //SectionID = 256;
	//***************************TEST CODE Index End**********************
    
    int SectionIndex = [self SearchSectionIdex:SectionID];
    
    if(SectionIndex >= [homeMode.initOB.mu_Arr_Slides count])
        return 0;
	
    
	return SectionIndex;
}

-(void)setUpdateStatus:(BOOL)shouldUpdate:(PageViewController*)sender{
    VC_Page = sender;
    if (!homeMode.initOB.Conf_Kiosk.enabled) {
        [updater setUpdateStatus:shouldUpdate];
    }
}

#pragma mark -
#pragma mark Start initialize

- (void)InitialSlideSession
{
    initOB = homeMode.initOB;
    
    contController = [[ContainerController alloc]init:initOB.PID UserFolderName:homeMode.cont_Username];
    
    NSLog(@"Notice Count %d" , [contController.ContainOB.Array_Notice count]);
    
	NSMutableArray *nilControllers = [[NSMutableArray alloc] init];
	
	for (unsigned i= 0; i < [initOB.mu_Arr_Slides count]; i++)
		[nilControllers addObject:[NSNull null]];
	
	self.slideVCSets = nilControllers;
	[nilControllers release];
    
    allPages = [[NSMutableArray alloc] init];
    
    if(initOB.Conf_Kiosk.enabled)
    {
        if(initOB.Conf_Kiosk.userSlide > 0)
            [self loadViewWithSlide:[self GetSlideIndexByID:initOB.Conf_Kiosk.userSlide] display:YES pageID:initOB.Conf_Kiosk.userPage];
        else
            [self loadViewWithSlide:0 display:YES pageID:0];
    }
    
    if([homeMode hasInternet]){
        if(!initOB.Conf_Kiosk.enabled){
            [self performSelectorInBackground:@selector(UpdateStatus) withObject:nil];
        } 
        
        if(initOB.hasSecondarySubscription){
            [self performSelectorInBackground:@selector(UpdateSecondary) withObject:nil];
        }
    } else {
        UIAlertView *alertMSG = [UIAlertView alloc];
        alertMSG = [alertMSG initWithTitle:@"Interactive Presentation No Connection"
								   message:@"The presentation you have selected is an interactive presentation, but there is no internet connection."
								  delegate:self 
						 cancelButtonTitle:@"OK" 
						 otherButtonTitles:nil];
		
		[alertMSG show];
        [alertMSG release];
        
    }
}

- (void)jumpSection:(int)SectionID
         oldSection:(int)OldSectionID
           jumpPage:(int)Page
{
    m_justJump = YES;
    
    if(SectionID != 0)
    {
        int oldSectionIndex = [self SearchSectionIdex:OldSectionID];
        int newSectionIndex = [self SearchSectionIdex:SectionID];
        
        NSLog(@"OldeSectionIndex:%d", oldSectionIndex);
        NSLog(@"NewSectionIndex:%d", newSectionIndex);
        
        SlideViewController *slideVC = [slideVCSets objectAtIndex:oldSectionIndex];
        if((NSNull*)slideVC != [NSNull null]){
            if((NSNull*)slideVC.userData != [NSNull null]){
                userData = slideVC.userData;
                [self updateUserData:SectionID];
            }
        }
        [self changeSlide:[NSNumber numberWithInt:newSectionIndex] pageID:Page];
            
    }
    else
        [self changeSlide:[NSNumber numberWithInt:0]];
}

- (void)updateUserData:(int)newSectionID
{
    int oldSectionID = [[[userData attributeForName:@"Section"]stringValue]intValue];
    
    if(oldSectionID != 0)
    {
        [[userData attributeForName:@"Section"]setStringValue:[NSString stringWithFormat:@"%d", newSectionID]];
        
        NSLog(@"NewUserSectionValue:%@",[[userData attributeForName:@"Section"]stringValue]);
    }
}

#pragma mark -
#pragma mark Handling idle timeout

- (void)resetIdleTimer 
{
    if (!idleTimer) {
        NSLog(@"ScheduleTime:%d", initOB.Conf_Kiosk.Timeout);
        
        idleTimer = [[NSTimer scheduledTimerWithTimeInterval:initOB.Conf_Kiosk.Timeout
                                                      target:self
                                                    selector:@selector(idleTimerExceeded)
                                                    userInfo:nil
                                                     repeats:NO]retain];
        //idleTimer
    }
    else {
        if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < initOB.Conf_Kiosk.Timeout-1.0) 
        {
            [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:initOB.Conf_Kiosk.Timeout]];
        }
    }
    
    m_idle = FALSE;
    
}

- (void)idleTimerExceeded 
{
    [idleTimer release]; idleTimer = nil;
    
    if(!m_idle){
        [self changeSlide:[NSNumber numberWithInt:[self SearchSectionIdex:initOB.Conf_Kiosk.attractSlide]]
                   pageID:initOB.Conf_Kiosk.attractPage];
    }
    [self resetIdleTimer];
    m_idle = TRUE;
    
    if (initOB.pagesVisited.count > 0) {
        [initOB.pagesVisited removeAllObjects]; 
    }

    
    if(initOB.Conf_Kiosk.idleMessage){
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSString *ACTIVEDECKKIOSK = [prefs stringForKey:@"Kiosk"];

        [self sendIdleMessage:[NSString stringWithFormat:initOB.Conf_Kiosk.channelFormat, [NSString stringWithFormat:@"%d", initOB.PID], [ACTIVEDECKKIOSK intValue]]];
    }
}

-(void)sendIdleMessage:(NSString*)channel{
    GDataXMLNode *defaultNamespace = [GDataXMLNode attributeWithName:@"xmlns" 
                                                         stringValue:@"http://www.activedeck.com/PowerPoint/"];
    
    GDataXMLElement *rootElement = [[GDataXMLElement alloc] initWithXMLString:@"<ActiveUpdate></ActiveUpdate>" error:nil];
    [rootElement addChild:defaultNamespace];
    
    GDataXMLElement *messageElement = [[GDataXMLElement alloc] initWithXMLString:[NSString stringWithFormat:@"<Message Channel=\"%@\"></Message>", channel] error:nil];
    
    NSString *idleXMLString = [[NSString alloc] initWithFormat:@"<Idle>%@</Idle>", initOB.Conf_Kiosk.idleMessage];
    
    [messageElement addChild:[[GDataXMLElement alloc] initWithXMLString:idleXMLString error:nil]];
    
    [rootElement addChild:messageElement];
    
    
    GDataXMLDocument *document = [[[GDataXMLDocument alloc] initWithRootElement:rootElement] autorelease];
    
    NSData *xData = document.XMLData;
    
    NSString *commandData = [[NSString alloc] initWithData:xData encoding:NSUTF8StringEncoding];
    
    HttpRequest *commandRequest = [[HttpRequest alloc] init];
    
    NSString *result = [commandRequest XML_PostRequest:commandData urlString:[NSString stringWithFormat:@"%@/publish/%d", initOB.webServiceEndPoint, initOB.PID]];
    
    if([result isEqualToString:@"Error"]){
//        message = [message initWithTitle:@"Error!" 
//                                 message:@"There was a problem publishing" 
//                                delegate:self 
//                       cancelButtonTitle:@"Please seek assistance." 
//                       otherButtonTitles:nil];
//        
//        [message show];
    }
}

-(BOOL)getIdleState{
    return m_idle;
}

#pragma mark -
#pragma mark Handling update timeout
#define UPDATECYCLE 600

- (void)resetUpdateTimer
{
    if(!updateTimer){
        NSLog(@"updateTimer Initialized");
        updateTimer = [[NSTimer scheduledTimerWithTimeInterval:UPDATECYCLE 
                                                        target:self 
                                                      selector:@selector(updateTimerExceeded) 
                                                      userInfo:nil 
                                                       repeats:NO]retain];
    }else{
        if (fabs([updateTimer.fireDate timeIntervalSinceNow]) < UPDATECYCLE -1) {
            [updateTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:UPDATECYCLE]];
        }
    }
}

- (void)updateTimerExceeded
{
    [updateTimer release];updateTimer = nil;
    
    if(m_idle)
    {
        if([homeMode AutoUpdateCheck])
        {
            [self newPresentation];
            homeMode.presentID.text = [NSString stringWithFormat:@"%d",initOB.PID];
            [homeMode DownloadPresentation];
        }
    }
    
    [self resetUpdateTimer];
}


-(void)unloadViews:(int)sIndex
{//&& i!= sIndex +1 && i !=sIndex -1
    for (int i=0 ; i< [slideVCSets count]; i++) {
        if(i != sIndex)
            [self unloadViewWithSlide:i];
    }
}

- (void)changeSlide:(NSNumber *)slideIndex
{
    if ([slideIndex intValue] >=[initOB.mu_Arr_Slides count] )
		return;
	
	AD_Slide *slideOB = [initOB.mu_Arr_Slides objectAtIndex:[slideIndex intValue]];
    
    if(!slideOB.Submit)
    {
        [self loadViewWithSlide:[slideIndex intValue] display:YES pageID:0];
        //[self loadViewWithSlide:[slideIndex intValue] + 1 display:NO pageID:0];
        // [self loadViewWithSlide:[slideIndex intValue] - 1 display:NO pageID:0];
        
        [self unloadViews:[slideIndex intValue]];
    }
}

- (void)changeSlide:(NSNumber *)slideIndex 
             pageID:(int)pageID
{
    NSLog(@"SlideIndex = %@, PageID = %d", slideIndex, pageID );
    [self loadViewWithSlide:[slideIndex intValue] display:YES pageID:pageID];
    //[self loadViewWithSlide:[slideIndex intValue] +1 display:NO pageID:pageID];
    //[self loadViewWithSlide:[slideIndex intValue] -1 display:NO pageID:pageID];
    
    [self unloadViews:[slideIndex intValue]];
}

- (void)notifySlideChange{
    hasMovedOn = YES;
    [VC_Page notifySlideChange];
}

- (void)loadViewWithSlide:(int)slideIndex 
                  display:(BOOL)show
                   pageID:(int)pageID{
    [self loadViewWithSlide:slideIndex display:show pageID:pageID :NO :!isGoingBackwards];
}

- (void)loadViewWithSlide:(int)slideIndex 
                  display:(BOOL)show
                   pageID:(int)pageID
                         :(BOOL)isForPDF
                         :(BOOL)recordToHistory
{	
    if (slideIndex >=[initOB.mu_Arr_Slides count] || slideIndex < 0 || slideIndex >= slideVCSets.count)
		return;
    
	AD_Slide *slideOB = [initOB.mu_Arr_Slides objectAtIndex:slideIndex];
	SlideViewController *slideVC = [slideVCSets objectAtIndex:slideIndex];
	
	if((NSNull *)slideVC == [NSNull null])
	{
		slideVC = [SlideViewController alloc];
		slideVC.PID = homeMode.presentID.text;
        slideVC.PresentName = homeMode.Lab_PresentName.text;
        slideVC.SlideOB = slideOB;
        slideVC.KioskOB = initOB.Conf_Kiosk;
		slideVC.PageOBs = slideOB.mut_Arr_Pages;
        slideVC.numberCorrect = numberCorrect;
        slideVC.numberIncorrect = numberIncorrect;
        slideVC.ContainOB = contController.ContainOB;
        slideVC.SlideIndex = slideIndex;
        slideVC.SlideCount = [slideVCSets count];
        slideVC.isForPDF = isForPDF;
        slideVC.canExportToPDF = initOB.canExportToPDF;
        
        int thePageID = pageID;
        for(AD_Page *p in slideOB.mut_Arr_Pages){
            if (p.oneTime) {
                thePageID = 0;
                break;
            }
        }
        
        slideVC.PageID = thePageID;
        
        if(userData != NULL)
        {
            slideVC.userData = userData;
            userData = NULL;
        }
        else
            slideVC.userData = homeMode.AuthNode;
        
        
        for (AD_Page *p in slideOB.mut_Arr_Pages) {
            if (![allPages containsObject:p]) {
                [allPages addObject:p];
            }
        }
        
        for (AD_Page *p in slideOB.mut_Arr_Pages) {
            if ([[p pageType] isEqualToString:@"ScoreReport"]){
                slideVC.allPages = [[NSMutableArray alloc] init];
                slideVC.allPages = allPages;
            }            
        }
        
        
        
        [slideVC setDelegate:self];
		
		[slideVCSets replaceObjectAtIndex:slideIndex withObject:slideVC];
        
        [slideVC release];
	} else {
        [slideVC jumpToPage:pageID];
    }
    
    [self recordNavigationHistory:recordToHistory :pageID :slideIndex];
	
	if(show)
	{
        if(!isForPDF){
            BOOL vcExist = nil == slideVC.view.superview;
            if(vcExist)
                [self.view addSubview:slideVC.view];
            else{
                [self.view bringSubviewToFront:slideVC.view];
                [slideVC viewDidAppear:TRUE];
            }
        } else {
            [self.view insertSubview:slideVC.view atIndex:2];
        }
	}
}

- (void)recordNavigationHistory:(BOOL)recordToHistory:(int)pageID:(int)slideIndex{
    if(recordToHistory && !isGoingBackwards){
        
        BOOL isNew = YES;
        if(initOB.pagesVisited.count > 0){
            SlidePagePair *prevSlidePagePair = [initOB.pagesVisited lastObject];
            if(prevSlidePagePair.pageID == pageID && prevSlidePagePair.slideID == slideIndex){
                isNew = NO;
            }
        }
        
        if(isNew){
            SlidePagePair *slidePagePair = [[SlidePagePair alloc] init];
            slidePagePair.pageID = pageID;
            slidePagePair.slideID = slideIndex;
            
            NSLog(@"******Recording Slide %d, Page %d", slidePagePair.slideID, slidePagePair.pageID);
            [initOB.pagesVisited addObject: slidePagePair];  
        }
    }
}

- (void)navigateBackwards{
    if(initOB.pagesVisited.count > 1){
        isGoingBackwards = TRUE;
        
        SlidePagePair *spp = [initOB.pagesVisited objectAtIndex:initOB.pagesVisited.count-2];
        SlidePagePair *lastPair = (SlidePagePair*)[initOB.pagesVisited lastObject];
        int oldSlide = lastPair.slideID;
        
        SlideViewController *slideVC = [slideVCSets objectAtIndex:spp.slideID];
        
        if(spp.slideID == oldSlide){
            if(slideVC && spp.pageID != 0){
                [slideVC jumpToPage:spp.pageID recordHistory:NO];                
            }
        } else {
            [self loadViewWithSlide:spp.slideID display:TRUE pageID:spp.pageID :NO :NO];
        }        
        
        [initOB.pagesVisited removeLastObject];
        
        isGoingBackwards = FALSE;
    }
}

- (void)unloadViewWithSlide:(int)slideIndex{
	if(slideIndex < 0 || slideIndex >= [initOB.mu_Arr_Slides count] )
		return;
	
	SlideViewController *slideVC = [slideVCSets objectAtIndex:slideIndex];
	if((NSNull *)slideVC != [NSNull null])
	{
		[slideVC.view removeFromSuperview];
		[slideVCSets replaceObjectAtIndex:slideIndex withObject:[NSNull	null]];
	}
}

-(void)updateAnswerCount:(int)numCorrect :(int)numIncorrect{
    numberCorrect = numCorrect;
    numberIncorrect = numIncorrect;
}

-(void)rotateForMovie:(BOOL)shouldRotate{
    rotateMovie = shouldRotate;
    if(!shouldRotate){
        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation( degreesToRadian(0) );
        [self.view setTransform:landscapeTransform];
        [self.view setFrame:CGRectMake(0, 20, 768, 1024)];
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    }
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(!rotateMovie){     
        rotateMovie = NO;
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    return rotateMovie;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [updater release];
    [homeMode release];
    [allPages release];
    [slideVCSets release];
    [initOB release];
    [super dealloc];
}

@end

@implementation SlidePagePair

@synthesize slideID, pageID;

@end
